import {
  Component,
  ViewChild,
  Output,
  Injector,
  EventEmitter,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineNodeServiceProxy,
  PipelineServiceProxy,
  MaterialOutServiceProxy,
  RequirementMaterialListDto,
  PipelineAndActsDetailDto,
  ActListDetailDto,
  SimpleTechDocListDto,
  MaterialOutListDto,
} from "@shared/service-proxies/service-proxies";
import { ModalDirective } from "ngx-bootstrap";
import { Paginator } from "primeng/components/paginator/paginator";
import { LazyLoadEvent } from "primeng/primeng";

@Component({
  selector: "stepDetailModal",
  templateUrl: "./step-detail-modal.component.html",
})
export class StepDetailModalComponent extends AppComponentBase {
  @ViewChild("actionModal") modal: ModalDirective;
  @ViewChild("paginator") paginator: Paginator;

  bills = new Array<RequirementMaterialListDto>();
  bill = new RequirementMaterialListDto();
  pipelineAndActsDetail = new PipelineAndActsDetailDto();
  pipelineAndActs = new Array<ActListDetailDto>();
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  techDocs = new SimpleTechDocListDto();
  outs = new Array<MaterialOutListDto>();
  active = false;
  saving = false;
  runing = false;
  isFullScreen = false;
  pipelineid: string;
  cols: any[];
  selectedColumns: any[];
  //modal 大小
  modalSize = "modal-xxl";
  event: LazyLoadEvent;
  subTaskCode = "";
  drawingCode = "";

  constructor(
    injector: Injector,
    private _pipelineTaskServiceProxy: PipelineNodeServiceProxy,
    private _pipelineService: PipelineServiceProxy,
    private _outMaterialServiceProxy: MaterialOutServiceProxy
  ) {
    super(injector);
  }
  show(pipelineid: string, event?: LazyLoadEvent): void {
    this.pipelineid = pipelineid;
    this.event = event;
    this.isFullScreen = false;
    this.active = true;
    this.cols = [
      {
        field: "taskCode",
        header: "任务号",
        width: "150px",
        isDefaultSelect: true,
      },
      {
        field: "drawingCode",
        header: "图号",
        width: "120px",
        isDefaultSelect: true,
      },
      {
        field: "materialName",
        header: "名称",
        width: "100px",
        isDefaultSelect: true,
      },
      { field: "invFurBatch", header: "炉批号", width: "100px" },
      {
        field: "invCode",
        header: "质检编号",
        width: "100px",
      },
      {
        field: "paiHao",
        header: "牌号",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "hgzNo",
        header: "合格证",
        width: "120px",
        isDefaultSelect: true,
      },
      { field: "hgz_zjd", header: "物质编码", width: "100px" },
      {
        field: "materialStatus",
        header: "材料状态",
        width: "100px",
        isDefaultSelect: true,
      },
      { field: "materialSpec", header: "规格", width: "100px" },
      { field: "materialSize", header: "尺寸", width: "100px" },
      { field: "invMeasName", header: "计量单位", width: "100px" },
      {
        field: "techCondition",
        header: "技术状态",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "hgz_zyd",
        header: "质疑单号",
        width: "100px",
        isDefaultSelect: true,
      },
      { field: "zhuanYongInfo", header: "专料专用信息", width: "100px" },
    ];

    this.selectedColumns = this.cols.filter((el) => {
      return el.isDefaultSelect;
    });
    this.requirementBill(pipelineid);
    this.getTechDocList(pipelineid);
    this.getOuts(pipelineid);
    //console.log(this.pipe);
    setTimeout(() => {
      this._pipelineService
        .getPipelineComponents(this.pipelineid)
        .subscribe((result) => {
          this.pipelineAndActsDetail = result;
          this.pipelineAndActs = result.acts;
          //console.log(this.pipelineAndActsDetail);
        });

      //this.loadComponent();
      this.modal.show();
    }, 0);
  }
  getTechDocList(pipelineid: string) {
    this._pipelineTaskServiceProxy
      .getTechDocList(pipelineid)
      .subscribe((result) => {
        this.techDocs = result.techDocs[0];
      });
  }
  getOuts(pipeid: string) {
    this._outMaterialServiceProxy
      .getOutWarehouseBills(pipeid)
      .subscribe((result) => {
        this.outs = result.items;
      });
  }
  fullScreen() {
    this.isFullScreen = !this.isFullScreen;
    setTimeout(() => {
      if (this.isFullScreen) {
        mApp.initScroller($(".m-scrollable-full-screen"), { height: "100%" });
      } else {
        $(".full-screen-content .mCSB_scrollTools").hide();
        $(".full-screen-content .mCustomScrollBox").removeClass(
          "mCustomScrollBox"
        );
        $(".full-screen-content .mCSB_container").removeClass("mCSB_container");
      }
    }, 0);
  }

  close(): void {
    //this.stepsComponent.getPipes();
    this.active = false;
    this.modal.hide();
    this.modalSave.emit(this.event);
  }

  requirementBill(pipelineid: string) {
    this._pipelineTaskServiceProxy
      .getRequirementBillByTask(pipelineid)
      .subscribe((result) => {
        //console.log(result);
        //this.bills = result.requirementMaterials;
        this.bill = result.requirementMaterials[0];
        this.subTaskCode = result.subTaskCode;
        this.drawingCode = result.drawingCode;
      });
  }
}
